import threading
import time
import logging


# lock = threading.Lock()

def log_set():
    logging.basicConfig(filename='log0304.txt',
                        level=logging.DEBUG,
                        filemode='a+',
                        format='%(message)s')


class testTheard():
    def watch_TV(self):
        for i in range(5):
            time.sleep(1)
            print('看电视')

    def do_homework(self):
        for i in range(5):
            time.sleep(1)
            print('做作业')

    def im_byqq(self):
        for i in range(5):
            time.sleep(1)
            print('聊天')


def watch_TV():
    global lock
    for i in range(5):
        lock.acquire()
        try:
            time.sleep(1)
            logging.info('看电视')
        finally:
            # 释放锁
            lock.release()


def do_homework():
    global lock
    for i in range(5):
        lock.acquire()
        try:
            time.sleep(1)
            logging.info('做作业')
        finally:
            # 释放锁
            lock.release()


def im_byqq():
    global lock
    for i in range(5):
        lock.acquire()
        try:
            time.sleep(1)
            logging.info('聊天')
        finally:
            # 释放锁
            lock.release()


# t = testTheard()
# print('执行前的时间：', time.asctime())
# t.watch_TV()
# t.do_homework()
# t.im_byqq()
# print('执行后的时间：', time.asctime())
lock = threading.Lock()
log_set()
print('执行前的时间：', time.asctime())
# 创建线程，并为这个线程指定一个任务
t = threading.Thread(target=watch_TV)
t2 = threading.Thread(target=do_homework)
t3 = threading.Thread(target=im_byqq)

t.start()
t2.start()
t3.start()

t.join()
t2.join()
t3.join()

print('执行后的时间：', time.asctime())
